$(function () {
    var getEmployListUrl = '/employ/getEmployList';
    //定义一页显示10行数据
    var pageSize = 10;
    //初始化显示第一页的内容
    var pageIndex = 1;
    //总员工数
    var maxItem = 999;
    //总页数
    var totalPage = 999;
    var role = '';
    var employName = '';
    var minSalary = '';
    var maxSalary = '';
    //初始化显示的内容
    showEmployList(role,employName,minSalary,maxSalary,pageIndex,pageSize);

    function showEmployList(role,employName,minSalary,maxSalary,pageIndex,pageSize) {
        var url = getEmployListUrl+'?pageIndex='+pageIndex+'&&pageSize='+pageSize
            +'&&role='+role+'&&employName='+employName +'&&minSalary='+ minSalary+'&&maxSalary='+maxSalary;
        $.getJSON(url,function (data) {
            maxItem = data.code;//获取的总数
            //添加分页的内容
            var employList = '';
            //先清空当前页码的内容
            $('#employList').empty();
            data.data.map(function (item, index) {
                var role = '';
                if (item.role == '0') {
                    role = '经理';
                }else if (item.role == '1') {
                    role = '销售';
                }else {
                    role = '后台管理人员';
                }
                var status = '';
                if (item.status == '0') {
                    status = '离职';
                }else if (item.status == '1') {
                    status = '在职';
                }
                employList += '<tr><td><input type="checkbox" name="id" value="'+item.id+'"   lay-skin="primary"></td>'
                    + '<td>'+item.id+'</td><td>'+item.employName+'</td>'
                    + '<td>'+item.idCard+'</td><td>'+item.phone+'</td><td>'+item.mail+'</td>' +'<td>'+item.gender+'</td>'
                    + '<td>'+item.salary+'</td>' +'<td>'+role+'</td>' + '<td>'+status+'</td>'
                    + '<td>'+new Date(item.entryTime).Format("yyyy-MM-dd")+'</td>'+'<td class="td-manage">'
                    + '<a title="查看" onclick="xadmin.open(\'编辑\',\'/frontend/employ-view?id='+item.id+'\')" href="javascript:;">'
                    + '<i class="layui-icon">&#xe63c;</i></a>' + '<a title="删除" onclick="member_del(this,\''+item.id+'\')" href="javascript:;">'
                    + '<i class="layui-icon">&#xe640;</i></a></td></tr>';
            });
            $('#employList').html(employList);
            //添加页数
            var pageNum = maxItem%pageSize==0?maxItem/pageSize:Math.ceil(maxItem/pageSize);
            totalPage = pageNum;
            $('#current').text(pageIndex);
            $('.end').text(totalPage);
            //role,employName,minSalary,maxSalary
            if (status !== '') {
                $('#status').val(status);
            }
            if (employName != '') {
                $('#employName').val(employName);
            }
            if (minSalary != '') {
                $('#minSalary').val(minSalary);
            }
            if (maxSalary != '') {
                $('#maxSalary').val(maxSalary);
            }

        });
    }
    //搜索员工内容
    $('#searchOrder').click(function () {
        var s1 = $('#status').val();
        var s2 = $('#employName').val();
        var s3 = $('#minSalary').val();
        var s4 = $('#maxSalary').val();
        if (s1 != '') {
            role = s1;
        }
        if (s2 != '') {
            employName = s2;
        }
        if (s3 != '') {
            minSalary = s3;
        }
        if (s4 != '') {
            maxSalary = s4;
        }
        showEmployList(role,employName,minSalary,maxSalary,pageIndex,pageSize);
    });

    //到最后一页
    $('.page .end').click(function () {
        var page = $(this).text();
        pageIndex = page;
        showEmployList(role,employName,minSalary,maxSalary,pageIndex,pageSize);
    });
    //前一页
    $('.layui-card-body .page .prev').click(function () {
        //如果当前页数为1则不能往前一页
        if (pageIndex>1){
            pageIndex = pageIndex-1;
            showEmployList(role,employName,minSalary,maxSalary,pageIndex,pageSize);
        }
    });
    //往后一页
    $('.page .next').click(function () {
        //如果当前页数为最后一页则不能往后一页
        if (pageIndex < totalPage) {
            pageIndex = pageIndex + 1;
            showEmployList(role,employName,minSalary,maxSalary,pageIndex,pageSize)
        }
    });
});


